package com.sjf.weibos.activity;

import java.util.HashMap;

import org.json.JSONObject;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.sjf.weibos.OAuth;
import com.sjf.weibos.WeiboFactory;
import com.sjf.weibos.WeiboFactory.TYPE;
import com.sjf.weibos.protocol.ProtocolUtil;

public class WebViewActivity extends BaseActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TYPE type=(TYPE) getIntent().getSerializableExtra("type");
        Log.e("TYPE", type.name());
        String requestUrl=getIntent().getStringExtra("requestUrl");
        WebView webView=(WebView)findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(requestUrl);
        webView.setWebViewClient(new WebViewClient(){

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				
				super.onPageStarted(view, url, favicon);
				final HashMap<String, String> access=OAuth.subString(url);
				Log.e("URL", url);
				Log.e("access", access.toString());
				if(access.containsKey("code")){
					WeiboFactory.getAccessToken(url, type, WebViewActivity.this);
				}
			}

			@Override
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				handler.proceed();
			}
        }
        );
    }

}
